//
//  MainViewController.m
//  UI_Project_A
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "MainViewController.h"
#import "ChannelViewController.h"
#import "MaInCollectionViewCell.h"
#import "MainCollectionVIewCellOfInt.h"
#import "HandleData.h"
#import "ModelOfData.h"
#import "ContectViewcontroller.h"
#import "WealthCollectionViewCell.h"
#import "PushWeathController.h"
#import "PictureViewCell.h"
#import "PicViewController.h"
#import "JokeCollectionViewCell.h"
#import "SearchViewController.h"
#import "SpeakViewController.h"
#import "GameCollectionViewCell.h"
#import "ModelOfGame.h"
#import "WebviewControl.h"
#import "AppDelegate.h"
#import <DKNightVersion.h>


@interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,MainCollectionVIewCellOfIntDelegate,Celldelegate>

@property (nonatomic ,retain)UICollectionView *collectionview;
@property (nonatomic ,retain)NSMutableArray *arrayTitle;
@property (nonatomic ,retain)UIScrollView *scrollview;
@property (nonatomic ,retain)UIButton *buttonCollection;
@property (nonatomic ,retain)UIView *viewOfNavi;
@property (nonatomic ,retain)UICollectionView *collectExten;
@property (nonatomic ,retain)UIView *viewOfMove;


@end

@implementation MainViewController

- (void)dealloc {
    
    [_arrayTitle release];
    [_collectionview release];
    [_scrollview release];
    [_buttonCollection release];
    [_viewOfNavi release];
    [_viewOfMove release];
    [super dealloc];
 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self createView];
    [self createScrollTitle];
    [self cracteCollectionview];
    [self createKVCvalue];
//    [self createButton];
    [self createsearch];
    [self createButtonStyle];
    [self createNotificationCenter];
    
 
}



#pragma mark - 上边的View
- (void)createView {

    
    
    self.viewOfNavi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 90)];
    [self.view addSubview:self.viewOfNavi];
    [self.viewOfNavi release];
    self.viewOfNavi.backgroundColor = [UIColor redColor];
    
    self.viewOfNavi.dk_backgroundColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:0.830 green:0.000 blue:0.000 alpha:1.000]);

}

#pragma mark -Button(个人设置)
- (void)createButtonStyle {
  
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:@"shezhi2"] forState:UIControlStateNormal];
    button.frame = CGRectMake(10, 25, 50, 30);
    [self.viewOfNavi addSubview:button];
    [button addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
  
}

- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
}





#pragma mark - 搜索

- (void)createsearch{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:@"sousuo3"] forState:UIControlStateNormal];
    button.frame = CGRectMake(WIDTH - 50, 25, 50, 30);
    
    [self.viewOfNavi addSubview:button];
    [button addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)search:(UIButton *)button {

    SearchViewController *search = [[SearchViewController alloc] init];
    [self presentViewController:search animated:YES completion:^{
        
    }];
    [search release];



}


#pragma mark - 添加Button(+)
- (void)createButton {
    self.buttonCollection = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonCollection.frame = CGRectMake(WIDTH - 50, 60, 50, 30);
    
    [self.buttonCollection setImage:[UIImage imageNamed:@"45"] forState:UIControlStateNormal];
    [self.buttonCollection addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.buttonCollection];
    [self.buttonCollection release];
    
    
    
}
#pragma mark - BUtton(+)实现

- (void)handleClick:(UIButton *)button {
 
    ChannelViewController *channel = [[ChannelViewController alloc]init];
    
    channel.arrayWithTitle = self.arrayTitle;
    
    [self presentViewController:channel animated:YES completion:nil];
    
    [channel release];
}


#pragma mark - 创建scroll
- (void)createScrollTitle {
   self.arrayTitle = @[@"互联网",@"财经",@"图片",@"话吧",@"游戏"].mutableCopy;
    
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, WIDTH, 50)];
  
    self.scrollview.pagingEnabled = YES;
    //滑动条
    self.scrollview.showsHorizontalScrollIndicator = NO;
    
    self.scrollview.contentSize = CGSizeMake(WIDTH/5 * self.arrayTitle.count, 0);
    self.scrollview.backgroundColor = [UIColor clearColor];
    [self.viewOfNavi addSubview:self.scrollview];
    [self.scrollview release];
    for (int i = 0; i < self.arrayTitle.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:self.arrayTitle[i] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(handleOffset:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = i+1;
        button.frame = CGRectMake(WIDTH/5 *i - 40, 0, WIDTH/5+80, 25);
        [self.scrollview addSubview:button];
        
    }
    //创建移动ViewOfmove
    [self createviewOfMove];
    
    
}

-(void)handleOffset:(UIButton *)button {

    NSIndexPath * path = [NSIndexPath indexPathForItem:button.tag - 1 inSection:0];
    
    [self.collectionview scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];


}

#pragma mark - ViewOfMove

- (void)createviewOfMove {
    self.viewOfMove = [[UIView alloc] init];
    self.viewOfMove.backgroundColor = [UIColor redColor];
    self.viewOfMove.frame = CGRectMake(30, 90, (WIDTH)/5-40, 2);
    [self.view addSubview:self.viewOfMove];
    [self.viewOfMove release];



}



#pragma mark -创建Collectionview

- (void)cracteCollectionview {
    
    //flowlayout
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowlayout.itemSize = CGSizeMake(WIDTH, HEIGHT-34);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    

    
    
    
    
    //collection
    self.collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0,92, WIDTH, HEIGHT-34) collectionViewLayout:flowlayout];
    self.collectionview.backgroundColor = [UIColor whiteColor];
    
    self.collectionview.pagingEnabled = YES;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:self.collectionview];
    
    //代理
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    //重用池
    [self.collectionview registerClass:[MaInCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    [self.collectionview registerClass:[MainCollectionVIewCellOfInt class] forCellWithReuseIdentifier:@"pool1"];
    [self.collectionview registerClass:[WealthCollectionViewCell class] forCellWithReuseIdentifier:@"pool2"];
    [self.collectionview registerClass:[PictureViewCell class] forCellWithReuseIdentifier:@"pool3"];
    [self.collectionview registerClass:[JokeCollectionViewCell class] forCellWithReuseIdentifier:@"pool4"];
    [self.collectionview registerClass:[GameCollectionViewCell class] forCellWithReuseIdentifier:@"pool5"];
    
    [self.collectionview release];
    [flowlayout release];
    
}


#pragma mark - CollectionDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.arrayTitle.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        MainCollectionVIewCellOfInt *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 1) {
        
        
        WealthCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool2" forIndexPath:indexPath];
        
        return cell;
    
    }else if (indexPath.row == 2) {
    
    
        PictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool3" forIndexPath:indexPath];
    
        return cell;
    
    }
    else if (indexPath.row == 3) {
    
        JokeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool4" forIndexPath:indexPath];
        
        return cell;
    }else {
    
        GameCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool5" forIndexPath:indexPath];
        
        cell.delegate = self;
        return cell;
    }
}






#pragma mark 协议方法
-(void)didClickWith:(ModelOfGame *)model{
    

    if ([model.video_url isEqualToString:@""]) {
//        NSLog(@"%@",model.idd);
        WebviewControl *web = [[WebviewControl alloc]init];
        
        NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/mobiles/item/%@?v_=400609&size=middle&t=1453899180",model.idd];
        web.urlstring = str;
        [self presentViewController:web animated:YES completion:nil];
//        [self.navigationController pushViewController:web animated:YES];
        
        [web release];
        
    }else{
        
        WebviewControl *web = [[WebviewControl alloc]init];
        
        NSString *str = model.video_url;
        web.urlstring = str;
        
        [self presentViewController:web animated:YES completion:nil];
//        [self.navigationController pushViewController:web animated:YES];
        
        
        [web release];
        
    }
}



#pragma mark - 点击Cell界面
-(void)pushContectViewWith:(ModelOfInternet *)model {
 
    ContectViewcontroller *contect = [[ContectViewcontroller alloc] init];
    
    contect.model = model;
    [self presentViewController:contect animated:YES completion:^{
        
    }];

}


#pragma mark - 创建通知中心
- (void)createNotificationCenter {
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"ineternet" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        ContectViewcontroller *contect = [[ContectViewcontroller alloc] init];
        
        contect.model = [note.userInfo objectForKey:@"content"];
        [self presentViewController:contect animated:YES completion:^{
            
        }];
    }];

    
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"push" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        PushWeathController *weath = [[PushWeathController alloc] init];
        
   weath.model = [note.userInfo objectForKey:@"content"];
                
        [self presentViewController:weath animated:YES completion:nil];
    }];
    
    
    //图片
    [[NSNotificationCenter defaultCenter] addObserverForName:@"picture" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        PicViewController * picture = [[PicViewController alloc] init];
        
        picture.model = [note.userInfo objectForKey:@"content"];
        
        [self presentViewController:picture animated:YES completion:nil];
        [picture release];
    }];
    
    
    //话题
    [[NSNotificationCenter defaultCenter] addObserverForName:@"questiontitle" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        SpeakViewController * speak = [[SpeakViewController alloc] init];
        speak.model = [note.userInfo objectForKey:@"content"];
        
        [self presentViewController:speak animated:YES completion:nil];
//        [speak release];
        
    }];
    
    
    
    
    
}






#pragma mark - 添加观察者属性

- (void)createKVCvalue {
    //在conection上添加观察着
    [self.collectionview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    CGFloat redX = (x-50) / 5;
 //   NSLog(@"%f",redX);
    //修改 redline 位置
    //修改 redline 位置
    self.viewOfMove.transform = CGAffineTransformMakeTranslation(redX, 0);
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
